package nl.weeaboo.sh.level1;

import static nl.weeaboo.sh.danmaku.SpellUtil.chain;
import static nl.weeaboo.sh.danmaku.SpellUtil.create;
import static nl.weeaboo.sh.danmaku.SpellUtil.tAngle;
import static nl.weeaboo.sh.danmaku.SpellUtil.tAngleInc;
import static nl.weeaboo.sh.danmaku.SpellUtil.tHp;
import static nl.weeaboo.sh.danmaku.SpellUtil.tPos;
import static nl.weeaboo.sh.danmaku.SpellUtil.tSolid;
import static nl.weeaboo.sh.danmaku.SpellUtil.tSpeed;

import java.awt.Point;
import java.awt.Rectangle;

import nl.weeaboo.game.input.UserInput;
import nl.weeaboo.sh.Game;
import nl.weeaboo.sh.danmaku.SpellUtil;
import nl.weeaboo.sh.danmaku.Universe;
import nl.weeaboo.sh.danmaku.UniverseAdapter;
import nl.weeaboo.sh.level.Level;
import nl.weeaboo.sh.objects.Boss;
import nl.weeaboo.sh.objects.Player;
import nl.weeaboo.sh.view.CharaImage;

public class Boss01 extends Boss {

	private int pattern;
	private int cooldown;
	private int moveFrame;
	private Point target;
	
	public Boss01(Game game, Level level) {
		super(game, level, game.getCharaImage("object/boss01.png#idle1"));
		
		startSpell(50, 120);
	}

	//Functions
	public void update(UserInput input, float dt) {
		UniverseAdapter ua = game.getLevel().getUniverseAdapter();

		if (ua != null) {		
			Universe u = ua.getUniverse();
			Rectangle r = ua.getBounds();
			
			cooldown--;
			moveFrame++;
			
			if (cooldown <= 0) {
				pattern++;
				if (pattern <= 4) {
					CharaImage i = game.getCharaImage("shot/shot01.png#blue1");
					for (float n = 0; n < 512; n += 4.0f) {				
						create(u, i, 0, chain(tPos(x - r.x, y - r.y), tAngle(n), tSpeed(5)));
					}
				} else {				
					Player player = level.getPlayer();
					float pa = SpellUtil.angleTo(x, y, player.getX(), player.getY());

					CharaImage i = game.getCharaImage("shot/shot01.png#rock1");
					create(u, i, 3, chain(tPos(x - r.x, y - r.y), tAngle(pa), tSpeed(1),
							tHp(100000), tSolid(true)));

					i = game.getCharaImage("shot/shot01.png#point1");
					create(u, i, 3, chain(tPos(x - r.x, y - r.y), tAngle(pa), tSpeed(2),
							tHp(100000), tSolid(true), tAngleInc(1)));
					
					pattern = 0;
				}
				
				cooldown = 50;
			}
	
			if (moveFrame == 240) {
				target = new Point(r.x + (int)(Math.random() * r.width),
						r.y + (int)(Math.random() * r.height));
			} else if (moveFrame > 240 && moveFrame < 360) {
				speedX += .1f * Math.signum(target.x - x);
				speedY += .1f * Math.signum(target.y - y);
				
				speedX = Math.max(-1, Math.min(1, speedX));
				speedY = Math.max(-1, Math.min(1, speedY));
			} else if (moveFrame == 360) {
				moveFrame = 239;
			}
		}
		
		speedY -= GRAVITY_ACCEL;
		super.update(input, dt);
	}
	
	//Getters
	
	//Setters
	
}
